package javaTest;
// 二叉树节点类
class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode(int val) {
        this.val = val;
    }
}
public class invertTree {
    // 翻转二叉树方法
    public TreeNode invertTree(TreeNode root) {
        // 递归终止条件：遇到空节点，直接返回
        if (root == null) {
            return null;
        }
        // 1. 递归翻转左子树
        TreeNode left = invertTree(root.left);
        // 2. 递归翻转右子树
        TreeNode right = invertTree(root.right);

        // 3. 交换当前节点的左右子树
        root.left = right;
        root.right = left;

        return root;
    }

}
